class Solution:
    def romanToInt(self, s: str) -> int:
        num={'I':1,
        'V':5,
        'X':10,
        'L':50,
        'C':100,
        'D' :500,
        'M':1000}
        pre = num[s[0]]
        a = pre
        for i in s[1:]:
            if num[i] > pre:
                a-=2*pre            
            a+=num[i]
            pre = num[i]

        return a
        
